package com.xizi.atomic;

import java.util.*;

class Person implements Comparable<Person>, Comparator<Person> {
    String name;
    int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return "[name = " + name + ", age = " + age + "]";
    }

    @Override
    public int compareTo(Person anthor) {
        if (this.age < anthor.age)
            return -1;
            //若年龄相等
        else if (this.age == anthor.age)
            //比较name 走的是String的compareTo
            return this.name.compareTo(anthor.name);
        else
            return 1;
    }

    @Override
    public int compare(Person o1, Person o2) {
        return o2.age - o1.age;
    }

    public static void main(String[] args) {
        List<Person> personLists = new ArrayList<Person>();
        personLists.addAll(Arrays.asList(new Person("戏子", 20), new Person("戏子zzz", 18), new Person("戏子zzzzzz", 0)));
        Collections.sort(personLists, new Person());
        System.out.println(personLists);
    }


}